<?php

namespace addons\xilumarket\controller;

use app\common\model\xilumarket\Config;
use app\common\model\xilumarket\OrderComment;
use app\common\model\xilumarket\Product;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Hook;

/**
 *  @title   定时任务
 */
class Time extends \think\addons\Controller
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
        if (!$this->request->isCli()) {
            $this->error('只允许在终端进行操作!');
        }
    }

    // 失效订单还原-商品状态还原
    public function order_cancel(){
        $lists = \app\common\model\xilumarket\Order::where('pay_status', '0')
            ->where('order_status','1')
            ->where('expiretime', 'lt', time())
            ->select();
        foreach ($lists as $list){
            $list->order_status = '2';
            $list->canceltime = time();
            $list->save();
            Product::where('id',$list->order_product[0]['product_id'])->inc('number')->update(['sale_out_status'=>'0']);
        }
    }
    // 订单自动收货
    public function order_receive(){
        $shopinfo = Config::getMyConfig('shopinfo');
        $auto_receipt_days = isset($shopinfo['auto_receipt_days'])?$shopinfo['auto_receipt_days']:15;
        $auto_times = time() - $auto_receipt_days*86400;
        $lists = \app\common\model\xilumarket\Order::where('pay_status', '1')
            ->where('order_status','1')
            ->where('receipt_status','0')
            ->where('freighttime', 'lt', $auto_times)
            ->select();
        foreach ($lists as $list){
            $list->receipt_status = '1';
            $list->receipttime = time();
            //$list->order_status = '3';
            $list->allowField(true)->save();
        }
    }

    // 订单默认好评
    public function order_comment(){
        $shopinfo = Config::getMyConfig('shopinfo');
        $auto_comment_days = isset($shopinfo['auto_comment_days'])?$shopinfo['auto_comment_days']:15;
        $auto_times = time() - $auto_comment_days*86400;
        $lists = \app\common\model\xilumarket\Order::where('pay_status', '1')
            ->where('order_status','1')
            ->where('receipttime', 'lt', $auto_times)
            ->select();
        foreach ($lists as $list){
            OrderComment::addComment([
                'user_id'=>$list->user_id,
                'order_id'=>$list->id,
                'product_id'=>$list->order_product?$list->order_product[0]['product_id']:0,
                'content'=>$shopinfo['auto_comment_content'] ?? '默认好评',
                'star'  => 5,
                'images'=>''
            ]);
        }
    }

    /**
     * 解冻-卖家佣金解冻
     */
    public function unfreeze(){
        $shopinfo = Config::getMyConfig('shopinfo');
        $auto_days = isset($shopinfo['commission_days'])?$shopinfo['commission_days']:7;
        $auto_times = time() - $auto_days*86400;
        $lists = \app\common\model\xilumarket\Order::where('pay_status', '1')
            ->where('freeze_status','0')
            ->where('receipt_status','1')
            ->where('receipttime', 'lt', $auto_times)
            // ->fetchSql()
            ->select();
        // var_dump($lists);die;
        Db::startTrans();
        try {
            foreach ($lists as $list){
                Hook::listen('xilumarket_thaw_divide',$list);
                $list->freeze_status = '1';
                $list->unfreezetime = time();
                $list->allowField(true)->save();
            }
        }catch (Exception|PDOException $e){
            var_dump("{$e->getFile()}[{$e->getLine()}] {$e->getMessage()}");
            Db::rollback();
            //return false;
        }
        Db::commit();
        //return true;
    }
}
